package com.cs456.TeamBobcat;

import java.io.File;

import com.cs456.TeamBobcat.database.MyDB;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class WhatDoesThisSay extends Activity implements OnClickListener {
	private static String TAG = "WhatDoesThisSay";

	protected Button _sendButton;
	protected ImageView _image;
	protected String _path;
   // MyDB dba;
	protected Intent _result;
	ProgressDialog _progressDialog;

	Thread _computeResult;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.whatdoesthissay);
//dba = new MyDB(this);
//dba.open();
		_image = (ImageView) findViewById(R.id.whatdoesthissay_image);
		_sendButton = (Button) findViewById(R.id.pictureButton);
		_sendButton.setOnClickListener(this);

		_result = new Intent(this, WhatDoesThisSayResult.class);

		startCameraActivity();
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.pictureButton:
			_progressDialog = ProgressDialog.show(WhatDoesThisSay.this, "",
					"Computing text. Please wait...", true);
			_computeResult = new Thread() {
				public void run() {
					WebService server = new WebService(WhatDoesThisSay.this);
					try{

						_result.putExtra(WebService.WHAT_DOES_THIS_SAY_RESULT_KEY,
								server.whatDoesThisSay(_path));
						_result.putExtra("picPath", _path);

						WhatDoesThisSay.this.runOnUiThread(new Runnable() {
							public void run() {
								_progressDialog.dismiss();
								startActivity(_result);
								_computeResult = null;
								finish();
							}
						});
					} catch (Throwable e){
						e.printStackTrace();
						WhatDoesThisSay.this.runOnUiThread(new Runnable() {
								public void run() {
									_progressDialog.dismiss();
									 Toast.makeText(WhatDoesThisSay.this, "A server error occured... X(", Toast.LENGTH_LONG).show();
									 finish();				
								}
							});
					}

				}
			};
			_computeResult.start();

			break;
		}
	}

	protected void startCameraActivity() {
		Log.i(TAG, "startCameraActivity()");
		Time now = new Time();
		now.setToNow();

		File dir = new File(Environment.getExternalStorageDirectory()
				+ "/Pictures/TeamBobcat/");
		dir.mkdirs();
		File file = new File(dir, now.format2445() + ".jpg");
		_path = file.getPath();
		Uri outputFileUri = Uri.fromFile(file);

		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
		//dba.insertWDTSphotos(_path, "HEllo World");
		//dba.close();
		startActivityForResult(intent, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i(TAG, "resultCode: " + resultCode);
		switch (resultCode) {
		case 0:
			Log.i(TAG, "User cancelled");
			// If no pictures have been collected
			startActivity(new Intent(WhatDoesThisSay.this, TeamBobcat.class));
			finish();
			break;

		case -1:
			onPhotoTaken();
			break;
		}
	}

	protected void onPhotoTaken() {
		Log.i(TAG, "onPhotoTaken");

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 4;

		Bitmap bitmap = BitmapFactory.decodeFile(_path, options);

		_image.setImageBitmap(bitmap);

	}

}